package org.authsum.stitches;


import org.codehaus.groovy.grails.commons.ConfigurationHolder
import java.io.*
import org.springframework.core.io.DefaultResourceLoader
import org.springframework.core.io.ResourceLoader
import org.springframework.core.io.Resource
import org.apache.commons.io.IOUtils
import org.authsum.stitches.*
import java.io.*
import org.springframework.core.io.*
import org.springframework.core.io.ResourceLoader
import org.springframework.core.io.Resource


import org.springframework.core.io.DefaultResourceLoader
import org.springframework.core.io.ResourceLoader
import org.springframework.core.io.Resource
import org.apache.commons.io.IOUtils
import org.authsum.stitches.external.*
import org.authsum.stitches.services.*;
class Helper {

		
	
	def stitchesService


		void sampleData() {
	 
	     
	     
			Long contentDefId = getContentDefId(name:"BootStrap example")
			
			
			
			//add binary attribute
			Long attributeId= getAttributeId(contentDefId:contentDefId,valueType:"Binary",mimeType:'image/jpeg')		
			
			//create content
		   for (i in 1..10) {
				Long contentId = getContentId(contentDefId:contentDefId,name:'Bootstrap created content ' + i)
				//create content version
				Long contentVersionId = getContentVersionId(contentId)
						//need to send contentVersionId
				String sampleImagePath= '/samplecontent/grails_logo.jpg'
				
				Long attributeValueId = getAttributeValueId(contentVersionId:contentVersionId,attributeId:attributeId,filePath:sampleImagePath)
				
		   }
			
			
			   for (i in 1..10) {
					Long tagId = getTagId(name:"Bootstrap Demo Tag " + i)
					findTagById(tagId)
					
			   }
			   
			   for (i in 1..10) {
					Long folderId = getFolderId(name:"Bootstrap Demo Folder " + i)
					
			   }
			   

			  
			  
		}

		
		void addFolderToContent(Long folderId,Long contentId) {


			def folderRequest =new FolderRequest()
			
			
			folderRequest.folderId = folderId
			folderRequest.contentId = contentId
			def response =stitchesService.addFolderToContent(folderRequest)
			response.messages?.string.each {
				println it
			}
			
			assert response.responseCode == '200'
		}
		
		void addTagToContent(Long tagId,Long contentVersionId) {

			def tagRequest = new TagRequest()
			tagRequest.tagId = tagId
			tagRequest.contentVersionId = contentVersionId
			def response =stitchesService.addTagToContent(tagRequest)
			assert response.responseCode == '200'
			
		}
		
		 Long getTagId(Map parms) {
			 
			def tagRequest = new TagRequest()
			def tagHolder = new TagHolder()
			
			if (!tagRequest.tagHolders) {
				def tagHolders = new org.authsum.stitches.external.ArrayOfTagHolder();
				tagRequest.tagHolders = tagHolders
				tagRequest.tagHolders.tagHolder.add(tagHolder)
			}
			
			tagHolder.name = parms.name
			
			
			def response =stitchesService.saveTag(tagRequest)
			
			assert response.responseCode == '200'
			def myth = response.tagHolders.tagHolder.get(0)
			assert myth.id > 0
			return myth.id	    
			
		}
		
		def findTagById(Long tagId)   {
			

			def tagRequest = new TagRequest()
			tagRequest.tagId = tagId
			def response =stitchesService.findTagById(tagRequest)
			
			assert response.responseCode == '200'
			
			def myth = response.tagHolders.tagHolder.get(0)
					assert myth.id > 0
			return myth
		}
		
		def findFolderById(Long folderId)   {
			
	
			def folderRequest = new FolderRequest()
			folderRequest.folderId = folderId
			def response =stitchesService.findFolderById(folderRequest)
			
			assert response.responseCode == '200'
			
			def myth = response.folderHolders.folderHolder.get(0)
					assert myth.id > 0
			return myth
		}
		
		
		
		Long getFolderId(Map parms) {
			
	
			def folderRequest = new FolderRequest()
			def folderHolder = new FolderHolder()
			
			
			if (!folderRequest.folderHolders) {
				def folderHolders = new ArrayOfFolderHolder()
				folderRequest.folderHolders = folderHolders
				folderRequest.folderHolders.folderHolder.add(folderHolder)
			}
			
			
			//folderRequest.folderHolder = folderHolder
			folderHolder.name = parms.name
			
			def response =stitchesService.saveFolder(folderRequest)
			assert response.responseCode == '200'
			
			def myfh = response.folderHolders.folderHolder.get(0)
			assert myfh.id > 0
			return myfh.id	    
			
		}
		
		
		public void addAttributeValueHolder(def request,Map args) {
			
			println "addAttributeValueHolder"
			def avh = new AttributeValueHolder()
			
			
			request.setContentVersionId(args.contentVersionId)
			
			avh.attributeId = args.attributeId
			
			if (args.filePath) {
				byte[] bytes = getBytes(args.filePath)
				avh.setBinaryData(bytes)
			}

			if (args.myIntegerValue){
				avh.valueAsInteger = args.myIntegerValue
			}
			
			if (args.latitude) {
				avh.latitude = args.latitude
			}
			if (args.longitude) {
				avh.longitude = args.longitude
			}
			
			
			avh.valueAsString = args.valueAsString
			
			if (request.attributeValueHolders  == null) {
				println "creating new ArrayOfAttributeValueHolder";
				def attributeValueHolders = new ArrayOfAttributeValueHolder()
				request.attributeValueHolders = attributeValueHolders
				

			}
			request.attributeValueHolders.attributeValueHolder.add(avh)
			
			
			
		}
		Long getAttributeValueId(Map args ) {
			
			//Long contentVersionId,Long attributeId,String filePath,Integer myIntegerValue,String myStringValue
			
			println "getAttributeValue contentVersionId=" + args.contentVersionId
			
			
			
			def request = new AttributeValuesRequest()

			
			
			
			
			addAttributeValueHolder(request,args)
			
			
			
			def response = stitchesService.saveAttributeValues(request)
			
			
			
			
			
			
			//TO DO, need to return the attribute value id, but it's not available yet....
			
			//last night, on reflection, i figured that I don't want to return attributevalueholders fromthe saveattributevalues call
			//since this would potentially involve resending back the very binary file that was just sent
			//so, instead, i will just not be able to return the attributeValueId's that have been created for teh saveAttributeValues call
			//and have to use the retrieveAttributeValue call  here to get my id
			
			
			request  = new AttributeValuesRequest()
			request.setContentVersionId(args.contentVersionId)


			println "here 1"
			if ( args.attributeId) {
				def attributeIds = new ArrayOfLong()
				request.attributeIds = attributeIds
				request.attributeIds.long.add( args.attributeId)
				//request.attributeIds.getLong().add(args.attributeId)
				//request.getAttributeIds().getLong().add(o)
			}


			
			
			println "here 2"
			def avresponse = stitchesService.retrieveAttributeValues(request)
						println "here 3"
			avresponse.messages.string.each {
				println it
			}
			println "here 4"
					assert avresponse.responseCode == '200'
						println "here 5"
			def myavh = avresponse.attributeValueHolders.attributeValueHolder.get(0)
						println "here 6"
			assert myavh.id > 0
			println "here 7"
			return myavh.id
			
			
			
		}
		
		
		
		byte[] getBytes(String resourcePath) throws MalformedURLException,
		IOException {
			
			Resource resource = new ClassPathResource(resourcePath,getClass().classLoader)
			//ResourceLoader resourceLoader = new DefaultResourceLoader();
			//Resource resource = resourceLoader.getResource(resourcePath);
			//File f = resource.getFile()
			InputStream is = resource.getURL().openConnection().getInputStream();
			byte[] bytes = IOUtils.toByteArray(is);
			return bytes;
			
		}
		
		
		
		
		 Long  getContentDefId(Map parms) {
			
			//Can't use the request/response for the unit test, can't reset parms
			
				
			def contentDefRequest = new ContentDefRequest()
			def contentDefHolder = new ContentDefHolder()
			contentDefRequest.contentDefHolder = contentDefHolder
			contentDefHolder.name = parms.name
			contentDefHolder.description = parms.name
			
			
			def contentDefResponse = stitchesService.saveContentDef(contentDefRequest)
			assert contentDefResponse.responseCode == '200'
			return contentDefResponse.contentDefHolders.contentDefHolder.get(0).id
			
			
			
		}
		
		
		 Long getContentId(Map parms) {
			assert parms.contentDefId !=  null
			assert parms.name !=  null
			
			
			
			def contentRequest = new ContentRequest()
			def contentHolder = new ContentHolder()
			
			
			
			contentRequest.contentHolder = contentHolder
			
			contentHolder.contentDefId = parms.contentDefId
			
			contentHolder.name = parms.name
			
			
			def contentResponse =stitchesService.saveContent(contentRequest)	
			contentResponse.messages.string.each {
				
				print "contentResponse " + it
			}
			assert contentResponse.responseCode == '200'
			Long contentId = contentResponse.contentHolders.contentHolder.get(0).id
			return contentId
		}
		

		 Long getAttributeId(Map parms) {		
			 println "getAttributeId"
			 
					
			def contentDefRequest = new ContentDefRequest()
			def attributeHolder = new AttributeHolder()
			contentDefRequest.attributeHolder = attributeHolder
			contentDefRequest.contentDefId = parms.contentDefId
			attributeHolder.name = parms.attributeName
			attributeHolder.description =parms.attributeName
			
				
				
				
			println  "parms.valueType=" + parms.valueType
			attributeHolder.valueType=parms.valueType
			attributeHolder.required=true
			
			attributeHolder.mimeType = parms.mimeType
			//attributeHolder.binaryType = parms.binaryType
			
			
			def contentDefResponse =stitchesService.addAttribute(contentDefRequest)	
			contentDefResponse.messages.string.each {
				
				print "hi " + it
			}
			println contentDefResponse.responseCode
			//assert contentDefResponse.responseCode == '200'
			return contentDefResponse.attributeHolder.id
		}
		
		
		Long getContentVersionId(Map args) {
			

			def contentVersionRequest = new ContentVersionRequest()
			def contentVersionHolder = new ContentVersionHolder()
			contentVersionRequest.contentVersionHolder =contentVersionHolder
			
			contentVersionRequest.contentVersionHolder.contentId = args.contentId   
			String contentVersionName = "another version"
			if (args.name) {
				contentVersionName = args.name
			}
			contentVersionRequest.contentVersionHolder.name = contentVersionName
			def response = stitchesService.saveContentVersion(contentVersionRequest)
			
			response.messages.each {
				println it
				
			}
			assert response.responseCode == "200" 		
			
			assert response.contentVersionHolders.contentVersionHolder.get(0).id > 0
			
			return  response.contentVersionHolders.contentVersionHolder.get(0).id 
			
		}
		
		void addChildFolder( Long folderId,Long childFolderId) {
			

			def folderRequest = new FolderRequest()
			
			
			folderRequest.setFolderId(folderId)
			folderRequest.setChildFolderId(childFolderId)
			
			def response = stitchesService.addChildFolder(folderRequest)
			assert response.responseCode == '200'
			
			
		}
		
}